package ui;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import util.Settings;

/**
 * Options menu.
 * @author VincentLy
 */
public class Options extends BasicGameState{

	private CustomSlider sample;
	private CustomButton back;	
	private String s1 = "Options";
	private float ex,ey = -1;

	public Options(){
		super();
		back = new CustomButton(Settings.screenX/2-Settings.buttonX/2,Settings.screenY-Settings.buttonY*2,"Back",'b');
		sample = new CustomSlider(100,300,"Music Volume: ",'s',.5f);
	}

	/**
	 * Initialization not needed yet.
	 * @author VincentLy
	 */
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {}

	/**
	 * Handles sliders and buttons on the menu.
	 * @author VincentLy
	 */
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		Input i = gc.getInput();
		ex = i.getMouseX();
		ey = i.getMouseY();
		if(back.contains(ex,ey)){
			back.setOn(true);
			if(i.isMousePressed(Input.MOUSE_LEFT_BUTTON))
				sbg.enterState(Settings.prevState);
		}	
		else
			back.setOn(false);
		if(i.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)){
			if(sample.tab().contains(ex,ey))
				sample.tab().setOn(true);
			if(sample.tab().on())
				sample.tab().move(ex,sample.tab().y());
			if(sample.tab().x()<sample.left())
				sample.tab().move(sample.left(),sample.tab().y());
			if(sample.tab().x()>sample.right())
				sample.tab().move(sample.right(),sample.tab().y());
		}
		else
			sample.tab().setOn(false);
		sample.update();
		gc.setMusicVolume(sample.val());
	}

	/**
	 * Draws sliders and other option ui components.
	 * @author VincentLy
	 */
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		g.setAntiAlias(true);
		g.setBackground(Settings.bg);
		g.setColor(Settings.screenText);
		g.drawString(s1,(Settings.screenX-g.getFont().getWidth(s1))/2,g.getFont().getHeight(s1));
		g.drawString(sample.text(),(Settings.screenX-g.getFont().getWidth(sample.text()))/2,sample.top()-sample.height()*8);
		sample.draw(g);
		back.draw(g);
	}

	/**
	 * @return id
	 * @author VincentLy
	 */
	public int getID() {return Settings.oID;}
}